<template>
  <DynamicDetailDialog
    v-model="visible"
    :title="`任务详情 - ${
      detailData?.task_name || detailData?.task_code || '加载中...'
    }`"
    :groups="detailGroups"
    :loading="loading"
    width="1200px"
    :close-on-click-modal="false"
  />
</template>

<script setup lang="ts">
import { computed } from "vue";
import DynamicDetailDialog from "@/components/DynamicDetailDialog";
import type { DetailGroup } from "@/components/DynamicDetailDialog/types";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  taskId: { type: Number, default: null },
  detailData: { type: Object, default: null },
  loading: { type: Boolean, default: false },
});

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value),
});

// 使用传入的详情数据和加载状态
const detailData = computed(() => props.detailData);
const loading = computed(() => props.loading);

// 获取场景类型文本
const getScenarioTypeText = (type: string): string => {
  const typeMap: Record<string, string> = {
    financial: "财务敏感信息",
    hr: "人事薪酬信息",
    contract: "商业合同",
    tech: "技术资料",
    customer: "客户信息",
  };
  return typeMap[type] || type;
};

// 获取任务类型文本
const getTaskTypeText = (type: string): string => {
  const typeMap: Record<string, string> = {
    quick: "快速扫描 (1页)",
    standard: "标准扫描 (3页)",
    deep: "深度扫描 (5页)",
  };
  return typeMap[type] || type;
};

// 获取状态文本
const getStatusText = (status: string): string => {
  const textMap: Record<string, string> = {
    running: "运行中",
    pending: "待执行",
    paused: "已暂停",
    completed: "已完成",
    failed: "已失败",
  };
  return textMap[status] || status;
};

// 获取状态类型
const getStatusType = (
  status: string
): "success" | "warning" | "danger" | "info" => {
  const typeMap: Record<string, "success" | "warning" | "danger" | "info"> = {
    running: "success",
    pending: "info",
    paused: "warning",
    completed: "success",
    failed: "danger",
  };
  return typeMap[status] || "info";
};

// 详情分组配置
const detailGroups = computed<DetailGroup[]>(() => {
  if (!detailData.value) return [];

  const groups: DetailGroup[] = [
    // 基本信息
    {
      title: "基本信息",
      column: 2,
      border: true,
      items: [
        {
          key: "task_code",
          label: "任务编码",
          value: detailData.value.task_code || "-",
        },
        {
          key: "task_name",
          label: "任务名称",
          value: detailData.value.task_name || "-",
        },
        {
          key: "main_keyword",
          label: "主关键词",
          value: detailData.value.main_keyword || "-",
        },
        {
          key: "scenario_type",
          label: "场景类型",
          value: detailData.value.scenario_type
            ? getScenarioTypeText(detailData.value.scenario_type)
            : "-",
        },
        {
          key: "task_type",
          label: "任务类型",
          value: detailData.value.task_type
            ? getTaskTypeText(detailData.value.task_type)
            : "-",
        },
        {
          key: "task_status",
          label: "任务状态",
          type: "tag",
          value: getStatusText(detailData.value.task_status),
          tagType: getStatusType(detailData.value.task_status),
          tagSize: "small",
        },
        {
          key: "description",
          label: "任务描述",
          value: detailData.value.description || "-",
          span: 2,
        },
        {
          key: "weight",
          label: "任务权重",
          value: detailData.value.weight || 1.0,
        },
      ],
    },
    // 调度配置
    {
      title: "调度配置",
      column: 2,
      border: true,
      items: [
        {
          key: "schedule_enabled",
          label: "启用调度",
          type: "tag",
          value: detailData.value.schedule_enabled ? "已启用" : "未启用",
          tagType: detailData.value.schedule_enabled ? "success" : "info",
          tagSize: "small",
        },
        {
          key: "cron_expression",
          label: "Cron表达式",
          value: detailData.value.cron_expression || "-",
        },
        {
          key: "next_run_time",
          label: "下次执行时间",
          value: detailData.value.next_run_time || "-",
        },
        {
          key: "timezone",
          label: "时区",
          value: detailData.value.timezone || "Asia/Shanghai",
        },
      ],
    },
    // 执行信息
    {
      title: "执行信息",
      column: 2,
      border: true,
      items: [
        {
          key: "last_run_time",
          label: "最后执行时间",
          value: detailData.value.last_run_time || "-",
        },
        {
          key: "last_run_status",
          label: "最后执行状态",
          type: "tag",
          value: detailData.value.last_run_status
            ? getStatusText(detailData.value.last_run_status)
            : "-",
          tagType: detailData.value.last_run_status
            ? getStatusType(detailData.value.last_run_status)
            : "info",
          tagSize: "small",
        },
        {
          key: "last_run_message",
          label: "最后执行消息",
          value: detailData.value.last_run_message || "-",
          span: 2,
        },
        {
          key: "last_run_duration",
          label: "最后执行耗时",
          value: detailData.value.last_run_duration
            ? `${detailData.value.last_run_duration} 秒`
            : "-",
        },
      ],
    },
    // 统计数据
    {
      title: "统计数据",
      column: 3,
      border: true,
      items: [
        {
          key: "total_scanned",
          label: "总扫描数",
          value: detailData.value.total_scanned || 0,
        },
        {
          key: "high_risk_count",
          label: "高风险",
          value: detailData.value.high_risk_count || 0,
        },
        {
          key: "medium_risk_count",
          label: "中风险",
          value: detailData.value.medium_risk_count || 0,
        },
        {
          key: "low_risk_count",
          label: "低风险",
          value: detailData.value.low_risk_count || 0,
        },
        {
          key: "alert_count",
          label: "告警数",
          value: detailData.value.alert_count || 0,
        },
        {
          key: "ticket_count",
          label: "工单数",
          value: detailData.value.ticket_count || 0,
        },
        {
          key: "confirmed_count",
          label: "已确认",
          value: detailData.value.confirmed_count || 0,
        },
        {
          key: "false_positive_count",
          label: "误报数",
          value: detailData.value.false_positive_count || 0,
        },
        {
          key: "success_rate",
          label: "成功率",
          value: detailData.value.success_rate
            ? `${detailData.value.success_rate.toFixed(1)}%`
            : "-",
        },
        {
          key: "avg_duration",
          label: "平均耗时",
          value: detailData.value.avg_duration
            ? `${detailData.value.avg_duration.toFixed(1)} 秒`
            : "-",
        },
      ],
    },
    // 时间信息
    {
      title: "时间信息",
      column: 2,
      border: true,
      items: [
        {
          key: "created_time",
          label: "创建时间",
          value: detailData.value.created_time || "-",
        },
        {
          key: "created_by",
          label: "创建者",
          value: detailData.value.created_by || "-",
        },
        {
          key: "updated_time",
          label: "更新时间",
          value: detailData.value.updated_time || "-",
        },
        {
          key: "updated_by",
          label: "更新者",
          value: detailData.value.updated_by || "-",
        },
      ],
    },
  ];

  // 扩展配置
  if (detailData.value.config) {
    const configItems: any[] = [];
    if (detailData.value.config.max_pages !== undefined) {
      configItems.push({
        key: "max_pages",
        label: "最大页数",
        value: detailData.value.config.max_pages,
      });
    }
    if (detailData.value.config.risk_threshold !== undefined) {
      configItems.push({
        key: "risk_threshold",
        label: "风险阈值",
        value: detailData.value.config.risk_threshold,
      });
    }
    if (detailData.value.config.auto_confirm !== undefined) {
      configItems.push({
        key: "auto_confirm",
        label: "自动确认",
        type: "tag",
        value: detailData.value.config.auto_confirm ? "是" : "否",
        tagType: detailData.value.config.auto_confirm ? "success" : "info",
        tagSize: "small",
      });
    }
    if (detailData.value.config.notification_enabled !== undefined) {
      configItems.push({
        key: "notification_enabled",
        label: "通知启用",
        type: "tag",
        value: detailData.value.config.notification_enabled
          ? "已启用"
          : "未启用",
        tagType: detailData.value.config.notification_enabled
          ? "success"
          : "info",
        tagSize: "small",
      });
    }

    if (configItems.length > 0) {
      groups.push({
        title: "扩展配置",
        column: 2,
        border: true,
        items: configItems,
      });
    }
  }

  return groups;
});
</script>

<style scoped lang="scss"></style>
